package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.myapplication.bean.User;

// 定义注册类
public class SigninActivity extends AppCompatActivity implements View.OnClickListener {
    //声明控件
    private EditText etName, etNumber, etNumber_fin;
    private Button btsignIn;
    private View re_register;
    private MySQLiteOpenHelper mMySQLiteOpenHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signin);

        // 初始化控件
        initViwe();
        mMySQLiteOpenHelper = new MySQLiteOpenHelper(this);

        // 找到并设置点击监听器
        // find找到layout布局文件activity_signin.xml的R.id.re_register并将其地址赋值给re_register对象
        re_register = findViewById(R.id.re_register);
        re_register.setOnClickListener(this);
    }

    // 分别获取layout的对象地址赋值给当前所在类的对象
    private void initViwe() {
        // 初始化控件
        etName = findViewById(R.id.re_username);
        etNumber = findViewById(R.id.re_password);
        etNumber_fin = findViewById(R.id.re_affirm);
        btsignIn = findViewById(R.id.re_register);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.re_register) {
            insert(v);
        }
    }

    // 往数据库添加用户和密码
    public void insert(View view){
        String name = etName.getText().toString().trim();
        String Number = etNumber.getText().toString().trim();
        String Number_fin = etNumber_fin.getText().toString().trim();

        User user = new User();

        // 如果输入的两次注册密码相等，则插入注册的用户名和密码到数据库的用户数据表
        if(Number.equals(Number_fin)) {
            user.setName(name);
            user.setNumber(Number);
            user.setNumber_fin(Number_fin);


            //插入数据库中
            long rowId = mMySQLiteOpenHelper.insertData(user);
        }
        // 如果输入的两次注册密码相等，则在layout界面弹出注册成功的消息提示框
        // 否则在layout界面弹出注册失败的消息提示框
        if(Number.equals(Number_fin)) {
            Toast.makeText(this,"注册成功！",Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this,"注册失败！",Toast.LENGTH_SHORT).show();
        }
    }
}
